package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Vacation;
import cn.edu.ctbu.lddweb.service.VacationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 请假管理Controller
 */
@Controller
public class VacationController {
    @Autowired
    private VacationService vacationService;

    //学生请假
    @RequestMapping("/vacation/index")
    public String index(Model model,String username,ModelMap map,Vacation vacation){
        vacation.setStudentName(username);
        List<Vacation> vacations = vacationService.selectList(vacation);
        model.addAttribute("vacation",vacations);

        map.put("username",username);
        return "/vacation/index";
    }

    //搜索/重置
    @RequestMapping("/selectVacation")
    public String selectVacation(@RequestParam(value = "type",required = false) String type,@RequestParam(value = "isAgree",required = false) String isAgree,@RequestParam(value = "username",required = false) String username, Model model,ModelMap mp){
        Vacation vacation = new Vacation();
        vacation.setType(type);
        vacation.setIsAgree(isAgree);
        vacation.setStudentName(username);
        List<Vacation> list = vacationService.selectList(vacation);
        model.addAttribute("vacation",list);

        mp.put("username",username);
        return "/vacation/index";
    }

    //新增页面
    @RequestMapping("/vacation/insert")
    public String insert(Model model,String username,ModelMap map){
        map.put("username",username);
        return "/vacation/insert";
    }

    //新增操作
    @RequestMapping("/insertVacation")
    public String insertVacation(Vacation vacation,ModelMap map){
        //获取当前时间并且格式化
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义新的日期格式
        String dateString = formatter.format(date);
        vacation.setDate(dateString);//提交请假的时间
        vacationService.insertVacation(vacation);

        map.put("username",vacation.getStudentName());
        return "/vacation/index";
    }


    //修改
    @RequestMapping("/vacation/edit")
    public String edit(int id,Model model){
        Vacation vacation = vacationService.findID(id);
        model.addAttribute("vacation",vacation);
        return "/vacation/edit";
    }

    //修改操作
    @RequestMapping("/updateVacation")
    public String updateVacation(Vacation vacation,ModelMap map){
        //获取当前时间并且格式化
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义新的日期格式
        String dateString = formatter.format(date);
        vacation.setDate(dateString);//当前修改点击提交的时间
        vacationService.updateVacation(vacation);

        map.put("username",vacation.getStudentName());
        return "/vacation/index";
    }

    //删除（管理员和学生公用的删除权限）
    @RequestMapping("/deleteVacation")
    public String deleteVacation(int id){
        vacationService.deleteVacation(String.valueOf(id));
        return "/vacation/index";
    }

    //管理员查看学生请假情况
    @RequestMapping("/vacation/indexroot")
    public String indexroot(Model model,String username,ModelMap map,Vacation vacation){
        List<Vacation> vacations = vacationService.selectList(vacation);
        model.addAttribute("vacation",vacations);

//        map.put("username",username);
        return "/vacation/indexroot";
    }

    //搜索/重置(管理员专用)
    @RequestMapping("/selectVacation2")
    public String selectVacation2(@RequestParam(value = "type",required = false) String type,@RequestParam(value = "isAgree",required = false) String isAgree, Model model){
        Vacation vacation = new Vacation();
        vacation.setType(type);
        vacation.setIsAgree(isAgree);
        List<Vacation> list = vacationService.selectList(vacation);
        model.addAttribute("vacation",list);
        return "/vacation/indexroot";
    }
}
